Console.Write Method (System) 您所在的位置:网站首页 write as控制 Console.Write Method (System)

Console.Write Method (System)

#Console.Write Method (System) | 来源: 网络整理| 查看: 265

Writes the text representation of the specified array of objects to the standard output stream using the specified format information.

public: static void Write(System::String ^ format, ... cli::array ^ arg); public static void Write (string format, params object?[]? arg); public static void Write (string format, params object[] arg); static member Write : string * obj[] -> unit Public Shared Sub Write (format As String, ParamArray arg As Object()) Parameters format String

A composite format string.

arg Object[]

An array of objects to write using format.

Exceptions IOException

An I/O error occurred.

ArgumentNullException

format or arg is null.

FormatException

The format specification in format is invalid.

Examples

The following example defines a Person class with a number of properties that provide information about a person. Its GetDescription method returns an array that contains all of the property values except one. The example then uses the array returned by the GetDescription method to display the values of the Person object.

using System; public class Person { public String Name { get; set; } public DateTime BirthDate { get; set; } public Double Height { get; set; } public Double Weight { get; set; } public Char Gender { get; set; } public String Remarks { get; set; } public object[] GetDescription() { return new object[] { Name, Gender, Height, Weight, BirthDate}; } } public class Example { public static void Main() { var p1 = new Person() { Name = "John", Gender = 'M', BirthDate = new DateTime(1992, 5, 10), Height = 73.5, Weight = 207 }; p1.Remarks = "Client since 1/3/2012"; Console.Write("{0}: {1}, born {4:d} Height {2} inches, Weight {3} lbs ", p1.GetDescription()); if (String.IsNullOrEmpty(p1.Remarks)) Console.WriteLine(); else Console.WriteLine("{1}Remarks: {0}", p1.Remarks, Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth ? "\n " : ""); } } // The example displays the following output: // John: M, born 5/10/1992 Height 73.5 inches, Weight 207 lbs Remarks: Client since 1/3/2012 open System type Person = { Name: string BirthDate: DateOnly Height: double Weight: double Gender: char Remarks: string } member this.GetDescription(): obj [] = [| this.Name; this.Gender; this.Height; this.Weight; this.BirthDate |] let p1 = { Name = "John" Gender = 'M' BirthDate = DateOnly(1992, 5, 10) Height = 73.5 Weight = 207 Remarks = "Client since 1/3/2012" } Console.Write("{0}: {1}, born {4:d} Height {2} inches, Weight {3} lbs ", p1.GetDescription()) if String.IsNullOrEmpty p1.Remarks then Console.WriteLine() else Console.WriteLine $"""{if Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth then "\n " else ""}Remarks: {p1.Remarks}""" // The example displays the following output: // John: M, born 5/10/1992 Height 73.5 inches, Weight 207 lbs Remarks: Client since 1/3/2012 Public Class Person Public Property Name As String Public Property BirthDate As DateTime Public Property Height As Double Public Property Weight As Double Public Property Gender As Char Public Property Remarks As String Public Function GetDescription() As Object() Return { Name, Gender, Height, Weight, BirthDate} End Function End Class Module Example Public Sub Main() Dim p1 As New Person() With { .Name = "John", .Gender = "M"c, .BirthDate = New DateTime(1992, 5, 10), .Height = 73.5, .Weight = 207 } p1.Remarks = "Client since 1/3/2012" Console.Write("{0}: {1}, born {4:d} Height {2} inches, Weight {3} lbs ", p1.GetDescription()) If String.IsNullOrEmpty(p1.Remarks) Then Console.WriteLine() Else Console.WriteLine("{1}Remarks: {0}", p1.Remarks, If(Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth, vbCrLf + " ", "")) End If End Sub End Module ' The example displays the following output: ' John: M, born 5/10/1992 Height 73.5 inches, Weight 207 lbs Remarks: Client since 1/3/2012

Note that the example calls the Write(String, Object[]) method rather than the WriteLine(String, Object[]) method because it attempts to display the value of the Person.Remarks property on the same line. To do this, it examines the value of the CursorLeft and WindowWidth properties to determine whether there is enough space for the remark to fit. If there is, it displays the line. If not, it writes a line, indents three spaces, and displays the remark.

The following example is identical to the first, except that it supplies a five-item list as the arg argument instead of a parameter array.

using System; public class Person { public String Name { get; set; } public DateTime BirthDate { get; set; } public Double Height { get; set; } public Double Weight { get; set; } public Char Gender { get; set; } public String Remarks { get; set; } public object[] GetDescription() { return new object[] { Name, Gender, Height, Weight, BirthDate}; } } public class Example { public static void Main() { var p1 = new Person() { Name = "John", Gender = 'M', BirthDate = new DateTime(1992, 5, 10), Height = 73.5, Weight = 207 }; p1.Remarks = "Client since 1/3/2012"; Console.Write("{0}: {1}, born {2:d} Height {3} inches, Weight {4} lbs ", p1.Name, p1.Gender, p1.BirthDate, p1.Height, p1.Weight); if (String.IsNullOrEmpty(p1.Remarks)) Console.WriteLine(); else Console.WriteLine("{1}Remarks: {0}", p1.Remarks, Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth ? "\n " : ""); } } // The example displays the following output: // John: M, born 5/10/1992 Height 73.5 inches, Weight 207 lbs Remarks: Client since 1/3/2012 open System type Person = { Name: string BirthDate: DateOnly Height: double Weight: double Gender: char Remarks: string } member this.GetDescription(): obj [] = [| this.Name; this.Gender; this.Height; this.Weight; this.BirthDate |] let p1 = { Name = "John" Gender = 'M' BirthDate = DateOnly(1992, 5, 10) Height = 73.5 Weight = 207 Remarks = "Client since 1/3/2012" } printf $"{p1.Name}: {p1.Gender}, born {p1.BirthDate:d} Height {p1.Height} inches, Weight {p1.Weight} lbs " if String.IsNullOrEmpty p1.Remarks then Console.WriteLine() else Console.WriteLine $"""{if Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth then "\n " else ""}Remarks: {p1.Remarks}""" // The example displays the following output: // John: M, born 5/10/1992 Height 73.5 inches, Weight 207 lbs Remarks: Client since 1/3/2012 Public Class Person Public Property Name As String Public Property BirthDate As DateTime Public Property Height As Double Public Property Weight As Double Public Property Gender As Char Public Property Remarks As String Public Function GetDescription() As Object() Return { Name, Gender, Height, Weight, BirthDate} End Function End Class Module Example Public Sub Main() Dim p1 As New Person() With { .Name = "John", .Gender = "M"c, .BirthDate = New DateTime(1992, 5, 10), .Height = 73.5, .Weight = 207 } p1.Remarks = "Client since 1/3/2012" Console.Write("{0}: {1}, born {2:d} Height {3} inches, Weight {4} lbs ", p1.Name, p1.Gender, p1.BirthDate, p1.Height, p1.Weight) If String.IsNullOrEmpty(p1.Remarks) Then Console.WriteLine() Else Console.WriteLine("{1}Remarks: {0}", p1.Remarks, If(Console.CursorLeft + p1.Remarks.Length + 10 > Console.WindowWidth, vbCrLf + " ", "")) End If End Sub End Module ' The example displays the following output: ' John: M, born 5/10/1992 Height 73.5 inches, Weight 207 lbs Remarks: Client since 1/3/2012 Remarks

This method uses the composite formatting feature of .NET to convert the value of an object to its text representation and embed that representation in a string. The resulting string is written to the output stream.

The format parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to an object in the parameter list of this method. The formatting process replaces each format item with the text representation of the value of the corresponding object.

The syntax of a format item is {index[,alignment][:formatString]}, which specifies a mandatory index, the optional length and alignment of the formatted text, and an optional string of format specifier characters that govern how the value of the corresponding object is formatted.

.NET provides extensive formatting support, which is described in greater detail in the following formatting topics.

For more information about the composite formatting feature supported by methods such as Format, AppendFormat, and some overloads of WriteLine, see Composite Formatting.

For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.

For more information about date and time format specifiers, see Standard Date and Time Format Strings and Custom Date and Time Format Strings.

For more information about enumeration format specifiers, see Enumeration Format Strings.

For more information about formatting, see Formatting Types.

The arg parameter is a parameter array. Arguments can be passed to the method either as an array or as list of five or more items. The examples illustrate both forms of method call.

Notes to Callers

This method is not called by C++ code. The C++ compiler resolves calls to System.Console.Write that include a string and a list of four or more object parameters as a call to Write(String, Object, Object, Object, Object). It resolves calls to System.Console.Write that include a string and an object array as a call to Write(String, Object).

See also Read() ReadLine() WriteLine() Formatting Types in .NET Composite Formatting Applies to


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有